<?php

/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\controller\adminapi\platform\work_wechat;


use app\controller\adminapi\platform\PlatformController;
use app\common\model\platform\Platform;
use app\common\model\platform\PlatformSetting;
use app\common\model\system\Config;
use EasyWeChat\MiniProgram\Mall\ForwardsMall;
use FormBuilder\Driver\CustomComponent;
use hiduoke\services\FormBuilder as Form;
use hiduoke\services\WorkWechatService;
use think\App;

class Setting extends PlatformController
{
    /**
     * @var PlatformSetting
     */
    protected $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->auth_type = (new WorkWechatService())->getAuthType();

        $this->alertForm = Form::ElAlert()->type('success')->showIcon(true)->title('已启用服务商配置')
            ->prop('style', 'margin-bottom: 20px;');
        $this->model = new PlatformSetting();
    }

    /**
     * 微信客服配置
     * @return \think\response\Json
     * @throws \FormBuilder\Exception\FormBuilderException
     */
    public function servicer()
    {
        $plaid = $this->request->param('plaid', 0);
        if ($this->request->isPost()) {
            $formData = $this->request->param('formData', []);
            $res = $this->model->setValueByKey($formData, "servicer", $plaid);
            return $this->success("修改成功", $res);
        } else {
//            $span = new CustomComponent($type);
            $h2 = new CustomComponent("h3");
            $rule = array();

            //创建表单
            $res = $this->model->getValueByKey("servicer", $plaid);
            $form = Form::createForm("/adminapi/platform/work_wechat/setting/servicer?plaid=" . $plaid)->setMethod("post");
            //新增表单
            if ($this->auth_type == 'open') {
                $rule[] = $this->alertForm->description('检测到您的企业微信配置为服务商授权，无需手动配置当前信息');
            } else {
                $rule[] = Form::dkInput('secret', 'Secret', $res['secret'] ?? '', "填写应用Secret，在应用管理-应用-微信客服-通过API管理-Secret")->required();
                $rule[] = $h2->appendChild("接收事件服务器")->prop("style", "margin-bottom:20px");
                $url = url('/'.config('hiduoke.work_wechat_app') . "/event/servicer", ['p' => get_plaid()], '', true)->build();
                $rule[] = Form::dkCopyInput("url", 'URL', $url)
                    ->appendRule("suffix", Form::getHelp("将当前地址填写至事件服务器URL，在应用管理-应用-微信客服-通过API管理-接收事件服务器-设置-URL"))
                    ->disabled(true);

                $rule[] = Form::input('token', 'Token', $res['token'] ?? '')
                    ->appendRule("suffix", Form::getHelp("服务端Token，在应用管理-应用-微信客服-通过API管理-接收事件服务器-设置-Token"))
                    ->required();
                $rule[] = Form::input('aes_key', 'Key', $res['aes_key'] ?? '')
                    ->appendRule("suffix", Form::getHelp("服务端EncodingAESKey，在应用管理-应用-微信客服-通过API管理-接收事件服务器-设置-EncodingAESKey"))
                    ->required();
            }
            $form->setTitle("")->setRule($rule);
            return $this->success("", Form::getFormData($form));
        }
    }

    /**
     * 客户联系配置
     * @return \think\response\Json
     * @throws \FormBuilder\Exception\FormBuilderException
     */
    public function customer()
    {
        $plaid = $this->request->param('plaid', 0);
        if ($this->request->isPost()) {
            $formData = $this->request->param('formData', []);
            $res = $this->model->setValueByKey($formData, "customer", $plaid);
            return $this->success("修改成功", $res);
        } else {
            $h2 = new CustomComponent("h3");
            $rule = array();
            //创建表单
            $res = $this->model->getValueByKey("customer", $plaid);
            $form = Form::createForm("/adminapi/platform/work_wechat/setting/customer?plaid=" . $plaid)->setMethod("post");
            //新增表单
            if ($this->auth_type == 'open') {
                $rule[] = $this->alertForm->description('检测到您的企业微信配置为服务商授权，无需手动配置当前信息,不过事件接收服务还是需要配置的');
                $rule[] = Form::dkInput('secret', 'Secret', $res['secret'] ?? '', "客户联系的凭证密钥，点击顶部【客户与上下游】-【api】-【Secret 查看 重新获取】")->disabled(true);
            }else{
                $rule[] = Form::dkInput('secret', 'Secret', $res['secret'] ?? '', "客户联系的凭证密钥，点击顶部【客户与上下游】-【api】-【Secret 查看 重新获取】")->required();
            }
            $rule[] = $h2->appendChild("接收事件服务器")->prop("style", "margin-bottom:20px");
            $url = url('/'.config('hiduoke.work_wechat_app') . "/event/customer", ['p' => get_plaid()], '', true)->build();
            $rule[] = Form::dkCopyInput("url", 'URL', $url)
                ->appendRule("suffix", Form::getHelp("将当前地址填写至事件服务器URL，在客户与上下游-api-接收事件服务器-设置-URL"))
                ->disabled(true);

            $rule[] = Form::input('token', 'Token', $res['token'] ?? '')
                ->appendRule("suffix", Form::getHelp("服务端Token，在客户与上下游-api-接收事件服务器-设置-Token"))
                ->required();
            $rule[] = Form::input('aes_key', 'Key', $res['aes_key'] ?? '')
                ->appendRule("suffix", Form::getHelp("服务端EncodingAESKey，在客户与上下游-api-接收事件服务器-设置-EncodingAESKey"))
                ->required();
            $form->setTitle("支付配置")->setRule($rule);
            return $this->success("", Form::getFormData($form));
        }
    }

}
